package com.qdlc.p2p.dal.dao.impl;

import java.util.List;

import javax.persistence.Query;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.qdlc.p2p.common.exception.UpRateException;
import com.qdlc.p2p.common.util.MessageUtil;
import com.qdlc.p2p.dal.common.dao.jpa.BaseDaoImpl;
import com.qdlc.p2p.dal.dao.RateRuleDetailDao;
import com.qdlc.p2p.dal.dto.RateRuleDetail;

/**
 * 加息券规则扩展DAO接口
 * @author wj
 * @version 1.0
 * @since 2015-04-22
 */
@Repository
public class RateRuleDetailDaoImpl extends BaseDaoImpl<RateRuleDetail> implements RateRuleDetailDao {
	
	private static Logger logger = Logger.getLogger(RateRuleDetailDaoImpl.class);  

    @SuppressWarnings("unchecked")
    @Override
    public RateRuleDetail getRetaRuleDetailById(long id) {
        String jpql = "from RateRuleDetail where id = ?1";
        Query query = em.createQuery(jpql);
        query.setParameter(1, id);
        List<RateRuleDetail> list = query.getResultList();
        if (list != null && list.size() >= 0) {
            return (RateRuleDetail) list.get(0);
        } else {
            return null;
        }
    }

	@SuppressWarnings("unchecked")
	@Override
	public List<RateRuleDetail> getrateDetailListByRid(long ruleId) {
		String jpql= "from RateRuleDetail where rateRuleId = ?1";
		Query query = em.createQuery(jpql);
		query.setParameter(1, ruleId);
		List<RateRuleDetail> list = query.getResultList();
		return list;
	}

	@Override
	public void updateRateDetailIsDelete(long id, int isDelete) {
		String jpql = "update  RateRuleDetail set isDelete = ?1 where id = ?2";
		Query query = em.createQuery(jpql);
		query.setParameter(1, isDelete);
		query.setParameter(2, id);
		int result  = query.executeUpdate();
		if (result !=1) {
			throw new UpRateException(MessageUtil.getMessage("MF002002001"));
		}
		
	}

	@Override
	public void deleteByRuleId(long ruleId) {
		String jpql = "delete from RateRuleDetail where rateRuleId = ?1";
		Query query = em.createQuery(jpql);
		query.setParameter(1, ruleId);
		int resule = query.executeUpdate();
		logger.info("删除父级ruleid："+ruleId+",影响条数："+resule);
		
	}
	
	
	
    
    
    
}
